const mintButton = document.querySelector('#mint-cards-btn');
const backButton = document.querySelector('#customization-tab-btn');
const mintSection = document.querySelector('#mint-cards');
const mintCardBank = document.querySelector("#mint-card-deck");
const connectButton = document.querySelector('#btn-connect-metamask');
const showRankingBtn = document.getElementById("show-btn");
var mintCards = [];
document.addEventListener('DOMContentLoaded', function() {
    let me = localStorage.getItem("address")
    if(me){
        let button = document.getElementById('btn-connect-metamask');
        button.textContent = me.substring(0,10) + "······" + me.substring(me.length-9,me.length);
    }
  });
function createCard(index, num, container_elem, cards) {
    let card_data = card_dict[index];

    let outer_elem = document.createElement("div");

    let elem = document.createElement("div");
    elem.style.backgroundImage = largeURL(card_data.deck + "_" + card_data.filename);
    elem.classList.add("card-lg");
    let count = document.createElement("div");

    let button = document.createElement("button");
    button.classList.add("mint-btn");
    button.textContent = "Mint Me"



    elem.appendChild(count);
    outer_elem.appendChild(elem);
    outer_elem.appendChild(button);

    container_elem.appendChild(outer_elem);

    let bankID = { index: index, count: num, elem: elem };
    let isBank = cards === this.bank;
    count.innerHTML = bankID.count;
    cards.push(bankID);
    let cardIndex = cards.length - 1;

    button.addEventListener("click", () => {
        var name = card_data.name.toLowerCase().replace(/\s/g, "") + Date.now()
        const data = {
            "publicKey": localStorage.getItem("publicKey"),
            "factoryId": "movies",
            "entityId": name,
            "recipientId": localStorage.getItem("address"),
            "secretKey": localStorage.getItem("secretKey")
        }
        axios.post("http://8.129.37.177:19003/transaction/trIssueEntity",data).then(res=>{
            console.log(res.data)
            if(!res.data.success){
                alert("上链失败")
            }else{
                alert("成功Minting_entityId:"+ name);
            }
        })

    });

    return bankID;
}

showRankingBtn.addEventListener("click", function() {
    const names = [
    {"address":"cEHm6xBtAK······xDdNprL9n", "score":parseInt(Math.random() * 5000)},
    {"address":"cAF2B2AaZG······oZ1UfUXkB", "score":parseInt(Math.random() * 5000)},
    {"address":"cKd63uDtep······juEWyWVTf", "score":parseInt(Math.random() * 5000)},
    {"address":"cEHGXyJrXi······UVmzqqt63", "score":parseInt(Math.random() * 5000)},
    {"address":"cEBeWVfJAF······D8H9kvFVr", "score":parseInt(Math.random() * 5000)},
    {"address":"cC1BavuQjZ······8Vo5NsGHw", "score":parseInt(Math.random() * 5000)},
    {"address":"cAF2B2AaZG······oZ1UfUXkB", "score":parseInt(Math.random() * 5000)},
    {"address":"cKd63uDtep······juEWyWVTf", "score":parseInt(Math.random() * 5000)},
    {"address":"cEHGXyJrXi······UVmzqqt63", "score":parseInt(Math.random() * 5000)},
    {"address":"cEa7WVfJAF······D8H9kvFVr", "score":parseInt(Math.random() * 5000)},
    {"address":"cC1BavuQjZ······8Vo5NsGHw", "score":parseInt(Math.random() * 5000)},
    {"address":"cAF2B2AaZG······oZ1UfUXkB", "score":parseInt(Math.random() * 5000)},
    {"address":"cKd63uDtep······juEWyWVTf", "score":parseInt(Math.random() * 5000)},
    {"address":"cEHGXyJrXi······UVmzqqt63", "score":parseInt(Math.random() * 5000)},
    {"address":"cEa7WVfJAF······D8H9kvFVr", "score":parseInt(Math.random() * 5000)},
    {"address":"cC1BavuQjZ······8Vo5NsGHw", "score":parseInt(Math.random() * 5000)},
    {"address":"cAF2B2AaZG······oZ1UfUXkB", "score":parseInt(Math.random() * 5000)},
    {"address":"cKd63uDtep······juEWyWVTf", "score":parseInt(Math.random() * 5000)},
    {"address":"cEHGXyJrXi······UVmzqqt63", "score":parseInt(Math.random() * 5000)},
    {"address":"cEa7WVfJAF······D8H9kvFVr", "score":parseInt(Math.random() * 5000)},
    {"address":"cC1BavuQjZ······8Vo5NsGHw", "score":parseInt(Math.random() * 5000)},
    {"address":"cAF2B2AaZG······oZ1UfUXkB", "score":parseInt(Math.random() * 5000)},
    {"address":"cKd63uDtep······juEWyWVTf", "score":parseInt(Math.random() * 5000)},
    {"address":"cEHGXyJrXi······UVmzqqt63", "score":parseInt(Math.random() * 5000)},
    {"address":"cEa7WVfJAF······D8H9kvFVr", "score":parseInt(Math.random() * 5000)},
    {"address":"cC1BavuQjZ······8Vo5NsGHw", "score":parseInt(Math.random() * 5000)},
    ];
    let me = localStorage.getItem("address")
    let scroe = parseInt(Math.random() * 5000)
    names.push({"address":me, "score":scroe},)
    names.sort((a, b)=>{
        return b.score - a.score
    })
    let namesString = names.map((a, index)=>{
        return "No."+ `${index + 1}` + ":  " + a.address + ": "+ a.score
    }).join("\n")
  
    // 显示弹窗
    alert("排行榜:\n\n" + namesString);
});
mintButton.addEventListener('click', () => {
    mintSection.removeAttribute("hidden");

});
connectButton.addEventListener('click', () => {
    const userInput = prompt('请输入你的助记词');
    if (userInput !== null) {
        axios.post("http://8.129.37.177:19003/sign/signIn", {"words":userInput}).then(res=>{
            connectButton.innerText = res.data.address.substring(0,10) + "······" + res.data.address.substring(res.data.address.length-9,res.data.address.length);
            localStorage.setItem("address", res.data.address);
            localStorage.setItem("secretKey", res.data.secretKey);
            localStorage.setItem("publicKey", res.data.publicKey);

        })
    }


});
backButton.addEventListener('click', () => {
    mintSection.setAttribute("hidden", true);

});

for (let i = 0; i <= 213; i++) {
    createCard(i, 1, mintCardBank, mintCards);

}